package com.jams.goodox.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jams.goodox.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jin on 2016/7/9.
 * Description **********星星星星星星星星星星
 */
public class StarsLayout extends RelativeLayout {

	// 只有亮的
	private final int NORMAL = 0;
	// 纯显示分数模式(有亮的暗的，但不能点击)
	private final int SCORE = 1;
	// 打分模式(有亮的暗的，能点击)
	private final int EDIT = 2;

	private RecyclerView recyclerView;
	private StarsLayoutAdapter mAdapter;
	private int blingType = NORMAL;
//	private boolean bigStar = false;
	private int starSize;

	List<Boolean> list;

	public StarsLayout(Context context, AttributeSet attrs) {
		super(context, attrs);

		View view = View.inflate(context, R.layout.stars_layout, null);

		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StarsLayout);

		try {
			int defaultSize = getResources().getDimensionPixelSize(R.dimen.star_space);
			float starSpace = a.getDimension(R.styleable.StarsLayout_starSpace, defaultSize);
//			bigStar = a.getBoolean(R.styleable.StarsLayout_bigStar, false);
			starSize = a.getDimensionPixelSize(R.styleable.StarsLayout_starSize, 0);

			// 控件类型
			blingType = a.getInteger(R.styleable.StarsLayout_blingType, NORMAL);

			recyclerView = (RecyclerView) view.findViewById(R.id.layout);
			list = new ArrayList<>();
			buildList(0);

			recyclerView.addItemDecoration(new SpaceItemDecoration((int) starSpace));
			recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
			mAdapter = new StarsLayoutAdapter(null);
			recyclerView.setAdapter(mAdapter);
			setBlingStar(0);

			if (blingType == EDIT)
				mAdapter.setOnRecyclerViewItemClickListener(new CustomRecyclerViewItemClickListener());
		} finally {
			a.recycle();
		}

		this.addView(view);
	}

	class StarsLayoutAdapter extends BaseQuickAdapter<Boolean> {

		public StarsLayoutAdapter(List<Boolean> list) {
			super(R.layout.stars_layout_item, list);
		}

		@Override
		protected void convert(BaseViewHolder baseViewHolder, Boolean b) {
			ImageView starImg = baseViewHolder.getView(R.id.star_img);
			if (starSize != 0) {
				ViewGroup.LayoutParams layoutParams = starImg.getLayoutParams();
				layoutParams.width = starSize;
				layoutParams.height = starSize;
				starImg.setLayoutParams(layoutParams);
			}
			if (blingType == SCORE || blingType == EDIT) {
				starImg.setBackgroundResource(b ? R.mipmap.my_ratingbar_selected : R.mipmap.my_ratingbar);
			} else {
				starImg.setBackgroundResource(R.mipmap.my_ratingbar_selected);
			}
//			if (blingType == SCORE || blingType == EDIT)
//				if (!bigStar) {
//					baseViewHolder.setBackgroundRes(R.id.star_img, b ? R.mipmap.yellow_star : R.mipmap.grey_star);
//				} else {
//					baseViewHolder.setBackgroundRes(R.id.star_img, b ? R.mipmap.yellow_star_big : R.mipmap.grey_star_big);
//				}
//
//			if (blingType == NORMAL)
//				if (!bigStar) {
//					baseViewHolder.setBackgroundRes(R.id.star_img, R.mipmap.yellow_star);
//				} else {
//					baseViewHolder.setBackgroundRes(R.id.star_img, R.mipmap.yellow_star_big);
//				}

		}
	}

	/**
	 * recyclerView item间距
	 */
	class SpaceItemDecoration extends RecyclerView.ItemDecoration {
		private int space;
		public SpaceItemDecoration(int space) {
			this.space = space;
		}

		@Override
		public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
			if (parent.getChildAdapterPosition(view) != 0) outRect.left = space;
		}
	}

	private int maxStar = 5;
	private int blingNum = 0;

	/**
	 * 设置最大星星数量
	 * @param maxStar 最大星星数量
	 */
	public void setMaxStarNumber(int maxStar) {
		this.maxStar = maxStar;
		setBlingStar(blingNum);
	}

	/**
	 * 设置黄星亮几个
	 * @param num 黄星亮几个
	 */
	public void setBlingStar(int num) {
		if (num > maxStar)
			num = maxStar;
		mAdapter.setNewData(buildList(num));
		if (listener != null) {
			listener.onStarsChanged(this, num);
		}
	}

	public int getBlingStar() {
		return blingNum;
	}

	/**
	 * 构建List
	 * @return List<Boolean>
	 */
	private List<Boolean> buildList(int i) {
		blingNum = i;
		list.clear();
		if (blingType == SCORE || blingType == EDIT) {
			for (int k = 0; k < maxStar; k++) {
				if (k < i)
					list.add(true);
				else
					list.add(false);
			}
			return list;
		} else if (blingType == NORMAL) {
			List<Boolean> list = new ArrayList<>(i);
			for (int k = 0; k < i; k++) {
				list.add(true);
			}
			return list;
		}
		return null;
	}

	class CustomRecyclerViewItemClickListener implements BaseQuickAdapter.OnRecyclerViewItemClickListener {

		@Override
		public void onItemClick(View view, int i) {
			setBlingStar(i + 1);
		}
	}

	public interface OnStarsLayoutChangeListener {
		void onStarsChanged(StarsLayout starsLayout, int stars);
	}

	private OnStarsLayoutChangeListener listener;

	public void setOnStarsLayoutChangeListener(OnStarsLayoutChangeListener listener) {
		this.listener = listener;
	}
}
